home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_03 / symtab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-08  |  2.9 KB  |  151 lines

  1. /* Symbol table manager for Bison,
  2.    Copyright (C) 1984 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. #include <stdio.h>
  22. #define USG 1
  23. #ifdef USG
  24. #include <string.h>
  25. #else /* NOT USG */
  26. #include <strings.h>
  27. #endif /* NOT USG */
  28.  
  29. #include "new.h"
  30. #include "gram.h"
  31. #include "symtab.h"
  32.  
  33. bucket **symtab;
  34. bucket *firstsymbol;
  35. bucket *lastsymbol;
  36.  
  37.  
  38. int
  39. hash(key)
  40. char *key;
  41. {
  42.   register char *cp;
  43.   register int k;
  44.  
  45.   cp = key;
  46.   k = 0;
  47.   while (*cp)
  48.     k = ((k << 1) ^ (*cp++)) & 0x3fff;
  49.  
  50.   return (k % TABSIZE);
  51. }
  52.  
  53.  
  54.  
  55. char *
  56. copys(s)
  57. char *s;
  58. {
  59.   register int i;
  60.   register char *cp;
  61.   register char *result;
  62.  
  63.   i = 1;
  64.   for (cp = s; *cp; cp++)
  65.     i++;
  66.  
  67.   result =  mallocate((unsigned int)i);
  68.   strcpy(result, s);
  69.   return (result);
  70. }
  71.  
  72.  
  73.  
  74. void tabinit()
  75. {
  76. /*   register int i; JF unused */
  77.  
  78.   symtab = NEW2(TABSIZE, bucket *);
  79.  
  80.   firstsymbol = NULL;
  81.   lastsymbol = NULL;
  82. }
  83.  
  84.  
  85.  
  86.  
  87. bucket *getsym(key)
  88. char *key;
  89. {
  90.   register int hashval;
  91.   register bucket *bp;
  92.   register int found;
  93.  
  94.   hashval = hash(key);
  95.   bp = symtab[hashval];
  96.  
  97.   found = 0;
  98.   while (bp != NULL && found == 0)
  99.     {
  100.       if (strcmp(key, bp->tag) == 0)
  101.     found = 1;
  102.       else
  103.     bp = bp->link;
  104.     }
  105.  
  106.   if (found == 0)
  107.     {
  108.       nsyms++;
  109.  
  110.       bp = NEW(bucket);
  111.       bp->link = symtab[hashval];
  112.       bp->next = NULL;
  113.       bp->tag = copys(key);
  114.       bp->class = SUNKNOWN;
  115.  
  116.       if (firstsymbol == NULL)
  117.     {
  118.       firstsymbol = bp;
  119.       lastsymbol = bp;
  120.     }
  121.       else
  122.     {
  123.       lastsymbol->next = bp;
  124.       lastsymbol = bp;
  125.     }
  126.  
  127.       symtab[hashval] = bp;
  128.     }
  129.  
  130.   return (bp);
  131. }
  132.  
  133.  
  134.  
  135. void free_symtab()
  136. {
  137.   register int i;
  138.   register bucket *bp,*bptmp;/* JF don't use ptr after free */
  139.  
  140.   for (i = 0; i < TABSIZE; i++)
  141.     {
  142.       bp = symtab[i];
  143.       while (bp)
  144.     {
  145.       bptmp=bp->link;
  146.       FREE(bp);
  147.       bp = bptmp;
  148.     }
  149.     }
  150. }
  151.